package org.ktlab.json.test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


public class HoangExtractor {
	public static void main(String args[]) throws InvalidFormatException, IOException{
		InputStream inp = new FileInputStream("data/hieunq.xls");
		File file = new File("hieunq.txt");
		FileWriter fw = new FileWriter(file);
		BufferedWriter bw = new BufferedWriter(fw);
	    //InputStream inp = new FileInputStream("workbook.xlsx");	   			
	    Workbook wb = WorkbookFactory.create(inp);
	    Sheet sheet = wb.getSheetAt(0);
	    for(Row row:sheet){
	    	if(row != null)
	    	for(int i=0;i<5;i++){
	    		String tmp="";
	    		Cell c2 = row.getCell(i);
	    		switch (c2.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    tmp = c2.getRichStringCellValue().toString().trim();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(c2)) {
                        tmp = c2.getDateCellValue().toString().trim();
                    } else {
                    	double t = c2.getNumericCellValue();
                    	int x = (int)t;
                        tmp = Integer.toString(x).trim();
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                   // System.out.println(c2.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    //System.out.println(c2.getCellFormula());
                    break;
                default:
                    //System.out.println();
            }
	    		//String tmp = cell.getRichStringCellValue().toString().trim();
	    		if(i==2){
	    			bw.write(tmp+"\t");
	    		}
	    		else if(i==4){
	    			bw.write(tmp);
	    		}
	    	}
	    	bw.write("\n");
	    }
	    bw.close();
	    fw.close();
	}
}
